package com.ruoyi.web.controller.moon;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.vo.UpdatePasswordVO;
import com.ruoyi.common.core.domain.vo.UserVoStatisticVo;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.ibatis.annotations.Update;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.entity.MoonUser;
import com.ruoyi.moon.service.IMoonUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户管理Controller
 * 
 * @author admin
 * @date 2024-11-05
 */
@RestController
@RequestMapping("/moon/subscriber")
public class MoonUserController extends BaseController
{
    @Autowired
    private IMoonUserService moonUserService;

    /**
     * 查询用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('moon:subscriber:list')")
    @GetMapping("/list")
    public TableDataInfo list(MoonUser moonUser)
    {
        startPage();
        List<MoonUser> list = moonUserService.selectMoonUserList(moonUser);
        return getDataTable(list);
    }

    /**
     * 导出用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('moon:subscriber:export')")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MoonUser moonUser)
    {
        List<MoonUser> list = moonUserService.selectMoonUserList(moonUser);
        ExcelUtil<MoonUser> util = new ExcelUtil<MoonUser>(MoonUser.class);
        util.exportExcel(response, list, "用户管理数据");
    }

    /**
     * 获取用户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('moon:subscriber:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(moonUserService.selectMoonUserById(id));
    }

    /**
     * 新增用户管理
     */
    @PreAuthorize("@ss.hasPermi('moon:subscriber:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MoonUser moonUser)
    {
        moonUser.setPassword(SecurityUtils.encryptPassword(moonUser.getPassword()));
        return toAjax(moonUserService.insertMoonUser(moonUser));
    }

    /**
     * 修改用户管理
     */
    @PreAuthorize("@ss.hasPermi('moon:subscriber:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MoonUser moonUser)
    {
        return toAjax(moonUserService.updateMoonUser(moonUser));
    }

    /**
     * 删除用户管理
     */
    @PreAuthorize("@ss.hasPermi('moon:subscriber:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(moonUserService.deleteMoonUserByIds(ids));
    }


    /**
     * 查封用户
     */
    @PostMapping("/sealUp")
    public AjaxResult sealUp(@RequestBody MoonUser moonUser)
    {
        return toAjax(moonUserService.UpdateStatus(moonUser));
    }


    /**
     * 后台管理修改密码
     */
    @PostMapping("/updatePassword")
    public AjaxResult updatePassword(@RequestBody UpdatePasswordVO updatePasswordVO)
    {
        updatePasswordVO.setPassword(SecurityUtils.encryptPassword(updatePasswordVO.getPassword()));
        return toAjax(moonUserService.updatePassword(updatePasswordVO));
    }

    @GetMapping("/statisticChannel")
    public AjaxResult statisticChannel(UserVoStatisticVo moonUser){

        UserVoStatisticVo statisticVo = moonUserService.statisticChannel(moonUser);
        List<Object> list = new ArrayList<>();
        Map map = new HashMap();
        map.put("name","PC");
        map.put("value",statisticVo.getPcCount());
        list.add(map);
        map = new HashMap();
        map.put("name","wechat");
        map.put("value",statisticVo.getWxCount());
        list.add(map);
        return AjaxResult.success(list);
    }
}
